package com.JobFind.controller;

import java.io.PrintWriter;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;
import com.JobFind.logic.IUserLogic;
import com.JobFind.DTO.UserDTO;

public class UserController extends ActionSupport {

	private static final long serialVersionUID = 1L;
	private IUserLogic userLogic;
	private UserDTO userDTO;
	
	public IUserLogic getUserLogic() {
		return userLogic;
	}
	@Resource
	public void setUserLogic(IUserLogic userLogic) {
		this.userLogic = userLogic;
	}
	public UserDTO getUserDTO() {
		return userDTO;
	}
	public void setUserDTO(UserDTO userDTO) {
		this.userDTO = userDTO;
	}
    
	public void loginCheck() throws Exception{
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		PrintWriter out=response.getWriter();
		try {
			out.println(this.userLogic.loginCheck(userDTO.getUserLoginName(), userDTO.getUserPwd()));
		} catch (Exception e) {
			e.printStackTrace();
		} finally{
			out.flush();
			out.close();
		}
	}
	
	public void getUserInfoById() throws Exception{
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		PrintWriter out=response.getWriter();
		try {
			out.println(this.userLogic.getUserInfoById(userDTO.getUserId()));
		} catch (Exception e) {
			e.printStackTrace();
		} finally{
			out.flush();
			out.close();
		}
	}
	
	public void logoutCheck() throws Exception{
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		PrintWriter out = response.getWriter();
		try {
			out.println(this.userLogic.logoutCheck(userDTO.getUserId()));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public void updateUserInfo() throws Exception{
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		PrintWriter out = response.getWriter();
		try {
			out.println(this.userLogic.updateUserInfo(userDTO.getUserId(),userDTO.getHomePlace(), userDTO.getUserBirthday(), userDTO.getUserNickName(), userDTO.getUserNation()));
		} catch (Exception e) {
			e.printStackTrace();
		} finally{
			out.flush();
			out.close();
		}
	}
	
	public void modifyUserPwd() throws Exception{
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		PrintWriter out = response.getWriter();
		try {
			out.println(this.userLogic.modifyUserPwd(userDTO.getUserId(), userDTO.getUserPwd()));
		} catch (Exception e) {
			e.printStackTrace();
		} finally{
			out.flush();
			out.close();
		}
	}

	public void regCheck() throws Exception{
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		PrintWriter out = response.getWriter();
		try {
			out.println(this.userLogic.addUser(userDTO));
		} catch (Exception e) {
			e.printStackTrace();
		} finally{
			out.flush();
			out.close();
		}
	}
}
